import { reqGetGoodsDetailInfo } from "@/api";

const state = {
  goodsDetailInfo:{},
};
const mutations = {
  SET_GOODSDETAILINFO(state,goodsDetailInfo){
    state.goodsDetailInfo = goodsDetailInfo;
  }
};
const actions = {
  async getGoodsDetailInfo({commit},skuId){
    try {
      const result = await reqGetGoodsDetailInfo(skuId);
      if (result.code === 200) {
        commit('SET_GOODSDETAILINFO',result.data);
      } else {
        alert('请求成功，但获取商品信息失败');
      }
    } catch (error) {
      alert('请求失败');
    }
  }
};
const getters = {
  categoryView(state){
    // 如果是undefined，就返回{}
    return state.goodsDetailInfo.categoryView || {};
  },
  skuInfo(state){
    return state.goodsDetailInfo.skuInfo || {};
  },
  spuSaleAttrList(state){
    return state.goodsDetailInfo.spuSaleAttrList || [];
  }
};

export default {
  namespaced:true,
  state,
  mutations,
  actions,
  getters,

}